//
//  topViewController.m
//  movie
//
//  Created by 李丹阳 on 15/7/17.
//  Copyright (c) 2015年 李丹阳. All rights reserved.
//

#import "topViewController.h"
#import "dataService.h"
#import "topModal.h"
#import "topCollectionViewCell.h"

#define Kwidth  self.view.bounds.size.width
#define Kheight self.view.bounds.size.height

@interface topViewController ()

@end

@implementation topViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"top250";
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self _initData];
    [self loadData];
    [self _createCollectionView];


}

- (void)_createCollectionView{
    
    //创建布局对象
    UICollectionViewFlowLayout* layout = [[UICollectionViewFlowLayout alloc]init];
    
    layout.itemSize = CGSizeMake((Kwidth-40)/3,(Kwidth-40)/3*1.5);//设计单元格大小
//    layout.minimumInteritemSpacing = 0;
//    layout.minimumLineSpacing = 10;
    
    //创建collectionView
    _topCollection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, Kwidth, Kheight) collectionViewLayout:layout];
    
    _topCollection.delegate = self;
    _topCollection.dataSource = self;
    
    //
    UINib* nib = [UINib nibWithNibName:@"topCollectionViewCell" bundle:[NSBundle mainBundle]];
    [_topCollection registerNib:nib forCellWithReuseIdentifier:@"cell"];
    
    [self.view addSubview:_topCollection];
}

- (void)_initData{
    _topModal = [[NSMutableArray alloc]init];
}

- (void)loadData{
    NSDictionary* dicData = [dataService getJSONDataFromFile:@"top250"];
    NSArray* subjects = [dicData objectForKey:@"subjects"];
    for (NSDictionary* dic in subjects) {
        topModal* modal = [[topModal alloc]init];
        modal.title = [dic objectForKey:@"title"];
        NSNumber* number = [[dic objectForKey:@"rating"] objectForKey:@"average"];
        modal.average = [number floatValue];
        modal.image = [dic objectForKey:@"images"];
//        [modal setValuesForKeysWithDictionary:<#(NSDictionary *)#>]
        [_topModal addObject:modal];

    }
//    NSLog(@"%@",_topModal);
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return [_topModal count];
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    topCollectionViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    topModal* top = _topModal[indexPath.row];
    cell.modal = top;
    return cell;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(10, 0,10, 10);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
